package com.itheima.reggie.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 自动填充类
 */@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    //使用MP添加操作时，该方法会自动执行，metaObject表示要添加的对象，如employee.category
    @Override
    public void insertFill(MetaObject metaObject) {
        //获取当前线程员工id
        Long employeeId = BaseContext.getCurrentId();

    //    this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
//        this.strictInsertFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
//        this.strictInsertFill(metaObject, "createUser", Long.class, 100L);
//        this.strictInsertFill(metaObject, "updateUser", Long.class, 100L);
    //最底层的方法
        metaObject.setValue("createTime",LocalDateTime.now());
        metaObject.setValue("updateTime",LocalDateTime.now());
        metaObject.setValue("createUser",employeeId);
        metaObject.setValue("updateUser",employeeId);

    }


    //使用MP修改操作时，该方法会自动执行，metaObject表示要修改的对象，如employee.category
    @Override
    public void updateFill(MetaObject metaObject) {
        //获取线程中本次员工的id
        log.info("当前线程的id:{}",Thread.currentThread().getId());
        Long employeeId = BaseContext.getCurrentId();
        //    this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
    //    this.strictInsertFill(metaObject, "updateUser", Long.class, 100L);
        //最底层的方法
        metaObject.setValue("updateTime",LocalDateTime.now());
        metaObject.setValue("updateUser",employeeId);
    }
}
